#include <string>
#include "UdpServer.hpp"
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char* argv[])
{
    if(argc != 3)
        return -1;

    //发送消息时OS自动帮客户端bind
    int sock = socket(AF_INET, SOCK_DGRAM, 0);

    std::string server_ip = argv[1];
    uint16_t server_port = atoi(argv[2]);

    struct sockaddr_in server;
    bzero(&server, sizeof(server));
    socklen_t len = sizeof(server);
    server.sin_family = AF_INET;
    server.sin_port = htons(server_port);
    server.sin_addr.s_addr = inet_addr(server_ip.c_str());

    std::string request;
    char buffer[1024] = {0};
    while(1)
    {
        std::cout << "Clinet request: " ;
        
        getline(std::cin, request);
        sendto(sock, request.c_str(), request.size(), 0, (const struct sockaddr*)&server, len);
        std::cout <<"发送了数据" << std::endl;

        int n = recvfrom(sock, buffer, sizeof(buffer) - 1, 0, (struct sockaddr*)&server, &len);
        buffer[n] = 0;
        std::cout << "Answer: " << buffer << std::endl;
    }
   
    return 0;
}




